package com.logicwin.aibala.system.sqlite;

import java.util.LinkedList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.logicwin.aibala.com.AibalaException;
import com.logicwin.aibala.com.GlobalConfig;
import com.logicwin.aibala.model.androidSWInfo.APKListItem;
import com.logicwin.aibala.model.androidSWInfo.AndroidSWInfoService;

/**
 * 数据库服务类
 * 
 * @author GuoDing
 * @since 2011-08-01
 */
public class SQLiteService {
	private SQLiteService() {
	}
	
	private static SQLiteDatabase myDB = null;
	
	static public void init(Context ctx) throws AibalaException {
		myDB = ctx.openOrCreateDatabase(GlobalConfig.getSQLiteDBName(), Context.MODE_PRIVATE, null);
		if(GlobalConfig.isFirstInstall()) {
			createTables();
		}
	}
	
	static public List<APKListItem> getAPKList(int classID, int sortID, int pageIndex) {
		List<APKListItem> ret = new LinkedList<APKListItem>();
		String table = null;
		
		switch(sortID) {
			case AndroidSWInfoService.SORT_ID_HOT:
				table = "HotAPKList";
				break;
			case AndroidSWInfoService.SORT_ID_TIME:
				table = "TimeAPKList";
				break;
			case AndroidSWInfoService.SORT_ID_RECOMMEND:
				table = "RecommandAPKList";
				break;
			default:
				break;
		}
		
		String[] columns = {"swID"};
		String selection = "where classID=? and pageIndex=pageIndex";
		String[] selectionArgs = {String.valueOf(classID), String.valueOf(pageIndex)};
		Cursor cursor = myDB.query(table, columns, selection, selectionArgs, null, null, "id");
		
		if(cursor.moveToNext()) {
			int swID = Integer.valueOf(cursor.getString(0));
			APKListItem ai = new APKListItem();
			ret.add(ai);
		}
		
		return ret;
	}
	
	static private void createTables() throws AibalaException {
		/**
		 * 软件列表信息表以及swID索引创建
		 */
		myDB.execSQL("CREATE TABLE IF NOT EXISTS APKListItem(id INTEGER(4) PRIMARY KEY AUTOINCREMENT," +
				                                            "swID INTEGER(4) UNIQUE," +
				                                            "apkName TEXT(32) NOT NULL," +
				                                            "versionCode TEXT(32) NOT NULL," +
				                                            "iconPicUrl TEXT(256) NOT NULL," +
				                                            "refStarCount INTEGER(4)," +
				                                            "downloadCount TEXT(16) NOT NULL," +
				                                            "apkSize TEXT(16) NOT NULL)");
		myDB.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS swID_Index ON APKListItem(swID)");
		
		/**
		 * 最新，最热，排行序列表
		 */
		myDB.execSQL("CREATE TABLE IF NOT EXISTS RecommandAPKList(id INTEGER(4) PRIMARY KEY AUTOINCREMENT," +
				                                           "swID INTEGER(4) UNIQUE," +
				                                           "classID INTEGER(4)," +
				                                           "pageIndex INTEGER(4))");
		myDB.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS RecommandAPKList_Index ON RecommandAPKList(pageIndex)");
		
		myDB.execSQL("CREATE TABLE IF NOT EXISTS HotAPKList(id INTEGER(4) PRIMARY KEY AUTOINCREMENT," +
                										   "swID INTEGER(4) UNIQUE," +
                										   "classID INTEGER(4)," +
                                                           "pageIndex INTEGER(4))");
		myDB.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS HotAPKList_Index ON HotAPKList(pageIndex)");

		myDB.execSQL("CREATE TABLE IF NOT EXISTS TimeAPKList(id INTEGER(4) PRIMARY KEY AUTOINCREMENT," +
                                                           "swID INTEGER(4) UNIQUE," +
                                                           "classID INTEGER(4)," +
                                                           "pageIndex INTEGER(4))");
		myDB.execSQL("CREATE UNIQUE INDEX IF NOT EXISTS TimeAPKList_Index ON TimeAPKList(pageIndex)");
	}
}














